package com.huaxin.hxmodulesystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huaxin.hxmodulesystem.dto.ResponseResult;
import com.huaxin.hxmodulesystem.dto.request.AddOrganizationReqDTO;
import com.huaxin.hxmodulesystem.dto.request.UpdateOrganizationReqDTO;
import com.huaxin.hxmodulesystem.dto.response.GetOrganizationInfoRespDTO;
import com.huaxin.hxmodulesystem.dto.response.OrganizationTreeNodeDTO;
import com.huaxin.hxmodulesystem.models.Organization;
import com.huaxin.hxmodulesystem.service.OrganizationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Tag(name = "组织")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/sys/organization")
public class OrganizationController {
    private final OrganizationService organizationService;

    /**
     * 新增组织
     *
     * @param in 新增组织入参
     * @return
     */
    @Operation(summary = "新增组织", description = "新增组织")
    @PostMapping("/addOrganization")
    public ResponseResult<Organization> addOrganization(@RequestBody AddOrganizationReqDTO in) {
        return ResponseResult.ok(organizationService.addOrganization(in));
    }

    /**
     * 根据组织ID获取组织信息
     *
     * @param id 组织ID
     * @return 组织基本信息
     */
    @Operation(summary = "根据组织ID获取组织信息", description = "根据组织ID获取组织信息")
    @GetMapping("/getOrganizationById")
    public ResponseResult<Organization> getOrganizationById(@Parameter(description = "组织ID", name = "id", required = true) @RequestParam(name = "id", required = true) String id) {
        return organizationService.getOrganizationById(id);
    }

    /**
     * 修改组织
     *
     * @param in 修改组织入参
     * @return
     */
    @Operation(summary = "修改组织", description = "修改组织")
    @PostMapping("/updateOrganization")
    public ResponseResult<String> updateOrganization(@RequestBody UpdateOrganizationReqDTO in) {
        return organizationService.updateOrganization(in);
    }

    /**
     * 删除组织
     *
     * @param id 组织ID
     * @return
     */
    @Operation(summary = "删除组织", description = "删除组织")
    @GetMapping("/deleteOrganization")
    public ResponseResult<String> deleteOrganization(@Parameter(description = "组织ID", name = "id", required = true) @RequestParam(name = "id", required = true) String id) {
        return organizationService.deleteOrganization(id);
    }

    /**
     * 获取组织列表
     *
     * @param pageNum  页码
     * @param pageSize 每页数量
     * @param search   搜索条件
     * @return
     */
    @Operation(summary = "获取组织列表", description = "获取组织列表")
    @GetMapping("/getOrganizationList")
    public ResponseResult<Page<Organization>> getOrganizationList(@Parameter(description = "页码", name = "pageNum", required = true) @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                                                  @Parameter(description = "每页数量", name = "pageSize", required = true) @RequestParam(name = "pageSize", required = true) Integer pageSize,
                                                                  @Parameter(description = "搜索条件", name = "search") @RequestParam(name = "search", required = false) String search) {
        return organizationService.getOrganizationList(pageNum, pageSize, search);
    }

    /**
     * 获取组织详情
     *
     * @param id 组织ID
     * @return
     */
    @Operation(summary = "获取组织详情", description = "获取组织详情")
    @GetMapping("/getOrganizationDetail")
    public ResponseResult<GetOrganizationInfoRespDTO> getOrganizationDetail(@Parameter(description = "组织ID", name = "id", required = true) @RequestParam(name = "id", required = true) String id) {
        return organizationService.getOrganizationDetail(id);
    }

    /**
     * 根据组织code获取组织信息
     *
     * @param code 组织code
     * @return
     */
    @Operation(summary = "根据组织code获取组织信息", description = "根据组织code获取组织信息")
    @GetMapping("/getOrganizationByCode")
    public ResponseResult<Organization> getOrganizationByCode(@Parameter(description = "组织code", name = "code", required = true) @RequestParam(name = "code", required = true) String code) {
        return ResponseResult.ok(organizationService.getOrganizationByCode(code));
    }

    /**
     * 绑定组织
     *
     * @param userId         用户ID
     * @param organizationId 组织ID
     * @return
     */
    @Operation(summary = "绑定组织", description = "绑定组织")
    @GetMapping("/bindOrganization")
    public ResponseResult<String> bindOrganization(@Parameter(description = "用户ID", name = "userId", required = true) @RequestParam(name = "userId", required = true) String userId,
                                                   @Parameter(description = "组织ID", name = "organizationId", required = true) @RequestParam(name = "organizationId", required = true) String organizationId) {
        return organizationService.bindOrganization(userId, organizationId);
    }

    /**
     * 解除组织绑定
     *
     * @param userId         用户ID
     * @param organizationId 组织ID
     * @return
     */
    @Operation(summary = "解除组织绑定", description = "解除组织绑定")
    @GetMapping("/unbindOrganization")
    public ResponseResult<String> unbindOrganization(@Parameter(description = "用户ID", name = "userId", required = true) @RequestParam(name = "userId", required = true) String userId,
                                                     @Parameter(description = "组织ID", name = "organizationId", required = true) @RequestParam(name = "organizationId", required = true) String organizationId) {
        return organizationService.unbindOrganization(userId, organizationId);
    }

    /**
     * 组织审核
     *
     * @param organizationId 组织ID
     * @param status         0-待审核 1-审核通过 2-审核未通过
     */
    @Operation(summary = "组织审核", description = "组织审核")
    @GetMapping("/auditOrganization")
    public ResponseResult<String> auditOrganization(@Parameter(description = "组织ID", name = "organizationId", required = true) @RequestParam(name = "organizationId", required = true) String organizationId,
                                                    @Parameter(description = "状态", name = "status", required = true) @RequestParam(name = "status", required = true) Integer status) {
        return organizationService.auditOrganization(organizationId, status);
    }

    //根据组织code判断组织是否存在
    @Operation(summary = "根据组织code判断组织是否存在", description = "根据组织code判断组织是否存在")
    @GetMapping("/queryOrganizationExist")
    public ResponseResult<Boolean> queryOrganizationExist(@Parameter(description = "组织code", name = "code", required = true) @RequestParam(name = "code", required = true) String code) {
        return organizationService.queryOrganizationExist(code);
    }

    /**
     * 获取组织树
     *
     * @return 组织树
     */
    @Operation(summary = "获取组织树", description = "获取组织树")
    @GetMapping("/getOrganizationTree")
    public ResponseResult<List<OrganizationTreeNodeDTO>> getOrganizationTree() {
        return ResponseResult.ok(organizationService.getOrganizationTree());
    }

    /**
     * 获取指定组织及其所有子组织的 ID 集合（含自身）
     *
     * @param orgId 组织ID
     * @return 包含自身的所有子孙组织ID集合
     */
    @Operation(summary = "获取指定组织及其所有子组织的 ID 集合（含自身）", description = "获取指定组织及其所有子组织的 ID 集合（含自身）")
    @GetMapping("/getOrgAndChildrenIds")
    public ResponseResult<Set<String>> getOrgAndChildrenIds(String orgId) {
        return ResponseResult.ok(organizationService.getOrgAndChildrenIds(orgId));
    }
}
